package com.edu.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName R
 * @date 2022/9/12 21:56
 * @Version 1.0
 */

@Data
public class R {

    @ApiModelProperty(value = "是否成功")
    private  boolean success;//是否成功

    @ApiModelProperty(value = "状态")
    private  Integer code;//状态

    @ApiModelProperty(value = "消息")
    private String message;//消息

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data=new HashMap<>(); //返回数据

    //把构造方法私有化
    private R(){}

    //返回成功静态方法
    public static  R ok(){
        R r = new R();
        r.setCode(Results.SUCCESS);
        r.setSuccess(true);
        r.setMessage("成功");
        return r;
    }

    //返回失败静态方法
    public static  R error(){
        R r = new R();
        r.setCode(Results.ERROR);
        r.setSuccess(false);
        r.setMessage("失败");
        return r;
    }

    // 链式调用
    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
